package com.bnz.service.impl;

import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.StudentService;
import com.bnz.util.MyBatisUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
public class StudentServiceImpl implements StudentService {
	//1. 查询所有学生
	@Override
	public List<Student> findAll() {
		try (SqlSession session = MyBatisUtil.getSqlSession()) {
			//1. 取得StudentMapper
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			//2. 查询所有学生
			List<Student> students = mapper.findAll();
			//3. 返回
			return students;
		}
	}

	//2. 分页查询
	@Override
	public PageResult<Student> findByPage(int page, int pagesize) {
		//2.1 开始分页
		PageHelper.startPage(page,pagesize);
		//2.2 开始查询
		try (SqlSession session = MyBatisUtil.getSqlSession()) {
			//1. 取得StudentMapper
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
			//2. 查询所有学生
			List<Student> students = mapper.findAll();
			//2.1 将学生与班级名称绑定
			for (Student student : students) {
				Classes classes = classesMapper.findById(student.getCid());
				student.setCname(classes.getCname());
			}
			//2.3 转换为Page<Student> 对象
			Page<Student> studentPage = (Page<Student>) students;
			//2.4 返回
			return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
		}
	}

	/**
	 * 3. 开始条件查询带分页功能
	 * @param page
	 * @param pagesize
	 * @param studentVo
	 * @return
	 */
	@Override
	public PageResult<Student> search(int page, int pagesize, StudentVo studentVo) {
		//3.1 开始分页
		PageHelper.startPage(page,pagesize);
		//3.2 开始查询
		try (SqlSession session = MyBatisUtil.getSqlSession()){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
			//3.1 开始条件查询
			List<Student> students = mapper.search(studentVo);
			//3.2 将学生与班级名称绑定
			for (Student student : students) {
				Classes classes = classesMapper.findById(student.getCid());
				student.setCname(classes.getCname());
			}
			//3.3 转换为Page<Student> 对象
			Page<Student> studentPage = (Page<Student>) students;
			//3.4 返回
			return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
		}
	}

	//4. 添加学生
	@Override
	public void insert(Student student) {
		try (SqlSession session = MyBatisUtil.getSqlSession()){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.insert(student);
		}
	}

	// 5. 修改学生
	@Override
	public void update(Student student) {
		try (SqlSession session = MyBatisUtil.getSqlSession()){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.update(student);
		}
	}

	// 6. 删除学生
	@Override
	public void delete(List<String> ids) {
		try (SqlSession session = MyBatisUtil.getSqlSession()){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.delete(ids);
		}
	}
}
